import { Role } from 'src/module/role/entities/role.entity';
import { PermissionType } from 'src/types';
import { Column, Entity, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Widget } from './widget.entity';

@Entity()
export class Permission {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true, length: 50, comment: '权限code' })
  code: string;

  @Column({ unique: true, length: 50, comment: '权限名称' })
  name: string;

  @ManyToOne(() => Permission, (permission) => permission.children, {
    createForeignKeyConstraints: false,
  })
  parent: Permission;

  @OneToMany(() => Permission, (permission) => permission.parent, {
    createForeignKeyConstraints: false,
  })
  children: Permission[];

  @Column({ nullable: true, comment: '父级id' })
  parentId: number;

  @Column({ nullable: true, comment: '路径' })
  path: string;

  @Column({ nullable: true, comment: '重定向地址' })
  redirect: string;

  @Column({ nullable: true, comment: 'icon图标' })
  icon: string;

  @Column({ nullable: true, comment: '组件' })
  component: string;

  @Column({ nullable: true })
  description: string;

  @Column({ comment: '国际化翻译' })
  translation: string;

  @Column({ default: true, comment: '是否展示在页面菜单' })
  show: boolean;

  @Column({ default: true })
  enable: boolean;

  @Column({ nullable: true })
  order: number;

  @ManyToMany(() => Role, (role) => role.permissions, {
    createForeignKeyConstraints: false,
  })
  roles: Role[];

  @OneToMany(() => Widget, (widget) => widget.permission)
  widgets: Widget[];
}
